#include "CapturingAndStoring.h"
#include "FacialRecognition.h"

const int CapturingAndStoring::FACE_WIDTH = 350;
const int CapturingAndStoring::FACE_HEIGHT = 350;
bool CapturingAndStoring::killThread = false;

CapturingAndStoring::CapturingAndStoring(void)
{
	croppedFaceImg = 0;
	killThread = false;
}

void CapturingAndStoring::PrepareFaceInImageAndStoreOnDisk(char * argv[], IplImage * inputImage, CvRect faceRect, std::string input, int personCounter, std::string dynInput, const char * filePath, bool personNameExists, std::vector<std::string> &vectorFilePaths)
{
	//Convert to greyscale
	if (inputImage->nChannels == 3) {
		resultImg = cvCreateImage( cvGetSize(inputImage), IPL_DEPTH_8U, 1 );
		// Convert from RGB (actually it is BGR) to Greyscale
		cvCvtColor(inputImage, resultImg, CV_BGR2GRAY);
	}
	else {
		resultImg = inputImage;
	}

	// Resize the image to be a consistent size, even if the aspect ratio changes
	imageProcessed = cvCreateImage(cvSize(resultImg->width, resultImg->height), IPL_DEPTH_8U, 1);

	// Make the image a fixed size
	// CV_INTER_CUBIC or CV_INTER_LINEAR is good for enlarging, and
	// CV_INTER_AREA is good for shrinking / decimation, but bad at enlarging.
	cvResize(resultImg, imageProcessed, CV_INTER_LINEAR);

	// Give the image a standard brightness and contrast.
	cvEqualizeHist(imageProcessed, imageProcessed);

	//Crop the image to show only the face area
	cvSetImageROI(imageProcessed, faceRect);

	//Save the cropped image
	croppedFaceImg = cvCreateImage(cvGetSize(imageProcessed), imageProcessed->depth, imageProcessed->nChannels);
	cvCopy(imageProcessed, croppedFaceImg, NULL);

	//Crop image to a fixed size
	cvSetImageROI(croppedFaceImg, cvRect(0, 0, FACE_WIDTH, FACE_HEIGHT));
	fixedImg = cvCreateImage(cvGetSize(croppedFaceImg), croppedFaceImg->depth, croppedFaceImg->nChannels);
	cvCopy(croppedFaceImg, fixedImg, NULL);
	cvResetImageROI(croppedFaceImg);

	//Make sure the fixed image has correct size
	if(fixedImg->width == FACE_WIDTH && fixedImg->height == FACE_HEIGHT
		&& faceRect.width >= CapturingAndStoring::FACE_WIDTH && faceRect.width <= CapturingAndStoring::FACE_WIDTH + 40 && 
		faceRect.height >= CapturingAndStoring::FACE_HEIGHT && faceRect.height <= CapturingAndStoring::FACE_HEIGHT + 40) {
		//int -> string
		char sizeTemp = (char)personCounter;
		char bufferTemp[sizeof(sizeTemp)/sizeof(char) + 10];
		std::string personNum = itoa(personCounter, bufferTemp, 10);
		dynInput = input + "\\" + "1.jpg";

		/*std::stringstream ss;
		std::string fullDynInput;
		ss << argv[1];
		ss >> fullDynInput;
		fullDynInput += "\\" + dynInput;*/

		filePath = dynInput.c_str();

		//Only save file path if person does not exist prior
		if(!personNameExists)
			vectorFilePaths.push_back(personNum + " " + dynInput);

		//Save image on disk
		cvSaveImage(filePath, fixedImg);
	}
	else {
		killThread = true;
	}
		
	//Display capturingAndStoring image
	//cvShowImage("capturingAndStoring", croppedFaceImg);

	if(croppedFaceImg)
		cvReleaseImage(&croppedFaceImg);

	if(fixedImg)
		cvReleaseImage(&fixedImg);

	if(resultImg)
		cvReleaseImage(&resultImg);

	if(imageProcessed)
		cvReleaseImage(&imageProcessed);
}

CapturingAndStoring::~CapturingAndStoring(void)
{
}
